import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { I18nService } from 'src/app/core/i18n/i18n.service';

@Component({
	templateUrl: './classifyAdd.html',
})
export class ClassifyAddComponent implements OnInit {
	validateForm;
	id;
	travelCategory;
	text;
	providerType = {};
	constructor(private i18n:I18nService ,private apiService: ApiService, private nzMessageService: NzMessageService, private fb: FormBuilder, private router: Router,private modalService: NzModalService,private activeRouter:ActivatedRoute) {
		this.id = this.activeRouter.snapshot.queryParams['id'];
		this.i18n.get('providerType').subscribe(res=>{
			this.providerType = res;
		})
	}
	ngOnInit() {
		this.text = this.id ? '编辑' : '创建';
		if(this.id){
			
			this.apiService.post('旅游分类详情',{id:this.id}).then((res:any)=>{
				this.travelCategory = res.data.travelCategory;
				this.validateForm.patchValue(this.travelCategory);
			})
		}
		this.validateForm = this.fb.group({
			providerType: [null, [Validators.required]],
			mode: ["DESIGNATED", [Validators.required]],
			name: [null, [Validators.required]],
			displayPriority: [null, [Validators.required]],
		});
	}
	submit() {
		for (const v in this.validateForm.controls) {
			this.validateForm.controls[v].markAsDirty();
			this.validateForm.controls[v].updateValueAndValidity();
		}
		if (this.validateForm.valid) {
			const url = this.id ? '编辑旅游分类':'创建旅游分类';
			const data = this.id ? {id:this.id,...this.validateForm.value} : this.validateForm.value;
			this.apiService.post(url, data).then((res: any) => {
				this.nzMessageService.success(this.text + '成功！');
				this.router.navigateByUrl('/basicSetting/classify');
			});
		}
	}
}
